<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>用户登陆</h2>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                       for="LAY-user-login-username"></label>
                <input type="text" name="account" id="LAY-user-login-username" lay-verify="required" placeholder="用户名"
                       class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                       for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required"
                       placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                               for="LAY-user-login-vercode"></label>
                        <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required"
                               placeholder="图形验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" id="sub-login" lay-submit lay-filter="LAY-user-login-submit">
                    登入
                </button>
            </div>
        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2018 服务网关管理系统<a href="#" target="_blank">鲁 ICP 备 xxx 号</a></p>
    </div>

</div>
<script>
    layui.use(['admin', 'form'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router()
            , search = router.search;

        var login = true;
        var apiUrl = setter.apiUrl;
        $(document).keydown(function (e) {
            if (e.keyCode == 13) {
                $('#sub-login').click();
            }
        });
        var imageCodeUrl = apiUrl + '/mapi/admin/img/code' + '?t=' + new Date().getTime();
        $('#LAY-user-get-vercode').attr('src', imageCodeUrl);

        //更换图形验证码
        $('#LAY-user-get-vercode').on('click', function () {
            var othis = $(this);
            this.src = apiUrl + '/mapi/admin/img/code' + '?t=' + new Date().getTime()
        });

        form.render();
        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {
            if (!login) {
                return;
            }
            login = false;
            ///img/verify/{code}
            //请求验证验证码
            var verifyUrl = apiUrl + '/mapi/admin/img/verify?code=' + obj.field.vercode;
            admin.req({
                url: verifyUrl
                , method: 'get'
                , xhrFields: {
                    withCredentials: true
                }
                , done: function (res) {
                    if (res.code == 0) {
                        var tokenUrl = apiUrl + '/mapi/admin/token';
                        //请求登入接口
                        admin.req({
                            url: tokenUrl
                            , method: 'post'
                            , data: JSON.stringify(obj.field)
                            , done: function (res) {
                                if (res.code == 0) {
                                    //请求成功后，写入 token
                                    layui.data(setter.tableName, {
                                        key: setter.request.tokenName, value: res.data.token
                                    });
                                    //请求成功后，写入 user
                                    layui.data(setter.tableName, {
                                        key: 'user', value: res.data
                                    });
                                    login = true;
                                    //登入成功的提示与跳转
                                    layer.msg('登陆成功', {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    }, function () {
                                        location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        offset: '15px', icon: 2
                                    });
                                    login = true;
                                }
                            }
                        });
                    } else {
                        layer.msg(res.msg, {
                            offset: '15px', icon: 2
                        });
                        login = true;
                    }
                }
            });

        });
    });
</script>